home *** CD-ROM | disk | FTP | other *** search
/ EuroCD 3 / EuroCD 3.iso / Programming / AMarquee / examples / AMarqueeServer.c < prev    next >
C/C++ Source or Header  |  1998-06-24  |  5KB  |  139 lines

  1.  
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5.  
  6. #include <dos/dos.h>
  7. #include <clib/dos_protos.h>
  8. #include <clib/exec_protos.h>
  9.  
  10. #include <clib/AMarquee_protos.h>
  11. #include <pragmas/AMarquee_pragmas.h>
  12.  
  13. FILE * output                 = NULL;
  14. struct Library * AMarqueeBase = NULL;
  15. struct QSession * session     = NULL;
  16.  
  17. #define UNLESS(x) if(!(x))
  18.  
  19. /* Takes user input until blank line is typed */
  20. void ProcessDebugCommands(struct QSession * session)
  21. {
  22.   BOOL BReturn = FALSE;
  23.   
  24.   while(BReturn == FALSE)
  25.   {
  26.    char debugline[500] = "\0\0\0\0\0\0\0\0";
  27.    char * keyword, * data;
  28.    ULONG dataLen = 0L;
  29.    LONG res;
  30.    
  31.    fprintf(output, "Enter your debug command now: "); fflush(output);
  32.    fgets(debugline, sizeof(debugline), output); fflush(output);
  33.    
  34.    keyword = &debugline[2];
  35.    if (data = strchr(keyword,'='))
  36.    {
  37.      *data = '\0';  /* terminate the keyword */
  38.      data++;
  39.      dataLen = strlen(data)+1;
  40.    }
  41.    switch((int)(debugline[0]))
  42.    {
  43.      case '\0': case '\n': res=QGo(session,0L); BReturn=TRUE;break;
  44.      case 's':  res=QSetOp(session, keyword, data, dataLen); break;
  45.      case 'm':  res=QMessageOp(session, keyword, data, dataLen); break;
  46.      case 'S':  res=QStreamOp(session, keyword, data, dataLen); break;
  47.      case 'd':  res=QDeleteOp(session, keyword);             break;
  48.      case 'i':  res=QInfoOp(session);                        break;
  49.      case 'p':  res=QPingOp(session);                        break;
  50.      default:   fprintf(output,"Command code %c was not recognized.\n",debugline[0]); break;
  51.    }
  52.    fprintf(output,"(Op result was %i)\n",res); fflush(output);
  53.   }
  54. }
  55.  
  56.  
  57. void CleanExit(void)
  58. {
  59.   if (session)      QFreeSession(session);        /* This MUST be done before we close the library! */
  60.   if (AMarqueeBase) CloseLibrary(AMarqueeBase);  
  61.   if (output)       
  62.   {
  63.     fprintf(output,"\n\nExiting... window will close in 2 seconds.\n"); fflush(output);
  64.     Delay(200);
  65.     fclose(output);
  66.   }
  67. }
  68.  
  69. /* Main program */
  70. int main(int argc, char ** argv)
  71. {    
  72.   UNLESS(output = fopen("CON:","r+")) exit(RETURN_ERROR);  
  73.  
  74.   atexit(CleanExit);
  75.  
  76.   UNLESS(AMarqueeBase = OpenLibrary("amarquee.library",37L))
  77.   {
  78.     fprintf(output, "Couldn't open amarquee.library v37!\n");
  79.     exit(RETURN_ERROR);
  80.   }
  81.   fprintf(output,"Getting connection socket from inetd...\n"); fflush(output);
  82.   UNLESS(session = QNewServerSession("#?", "#?"))
  83.   {
  84.     fprintf(output,"Couldn't accept connection from inetd.\n\n");
  85.     fprintf(output,"Note that this program MUST be started by AmiTCP,\n");
  86.     fprintf(output,"starting it manually from the CLI will only get you\n");
  87.     fprintf(output,"this message.  See the AMarquee docs under example\n");
  88.     fprintf(output,"programs for how to install and use this demo.\n");
  89.     exit(RETURN_WARN);
  90.   }
  91.  
  92.   fprintf(output, "This is a demonstration AmiTCP inetd daemon, using\n");
  93.   fprintf(output, "amarquee.library.  You may interactively send and receive\n");
  94.   fprintf(output, "data with any connecting AMarquee-based client.\n\n");
  95.   fprintf(output, "Commands for this demo daemon:\n");
  96.   fprintf(output, "\n");
  97.   fprintf(output, "s path=value  (use QSetOp to queue data)\n");
  98.   fprintf(output, "m hosts=value (use QMessageOp to queue data)\n");
  99.   fprintf(output, "S path=value  (use QStreamOp to queue data)\n");
  100.   fprintf(output, "d path        (use QDeleteOp to queue null data)\n");
  101.   fprintf(output, "i             (use QInfoOp to queue free memory info)\n");
  102.   fprintf(output, "p             (use QPingOp to queue ping packet\n");
  103.   fprintf(output, "<enter>       (Send all queued transactions)\n");
  104.   fprintf(output,"Press CTRL-F to enter commands, or CTRL-C to quit\n\n"); fflush(output);
  105.   while(1)
  106.   {
  107.     struct QMessage * qMsg;
  108.     ULONG signals = (1L << session->qMsgPort->mp_SigBit) | (SIGBREAKF_CTRL_C) | (SIGBREAKF_CTRL_F);
  109.  
  110.     /* Wait for next message from the server */
  111.     signals = Wait(signals);
  112.     
  113.     if (signals & (1L << session->qMsgPort->mp_SigBit))
  114.     {
  115.       while(qMsg = (struct QMessage *) GetMsg(session->qMsgPort))
  116.       {
  117.         struct QRunInfo * inf = (qMsg->qm_DataLen == sizeof(struct QRunInfo)) ? (struct QRunInfo *) qMsg->qm_Data : NULL;
  118.  
  119.         /* Handle message */
  120.         fprintf(output,"Message %p recieved---------\n",qMsg);
  121.         fprintf(output,"Status:       %i\n",  qMsg->qm_Status);
  122.         fprintf(output,"Error Line:   %i\n",  qMsg->qm_ErrorLine);
  123.         fprintf(output,"Message ID:   %i\n",  qMsg->qm_ID);
  124.         fprintf(output,"Path:        [%s]\n", qMsg->qm_Path?qMsg->qm_Path:"<NULL>");
  125.         fprintf(output,"Data:        [%s](int=%i)\n", qMsg->qm_Data?qMsg->qm_Data:((UBYTE*)"<NULL>"),qMsg->qm_Data?(*((int*)qMsg->qm_Data)):0);
  126.         fprintf(output,"DataLen:      %lu\n", qMsg->qm_DataLen);
  127.         fprintf(output,"ActualLen:    %lu\n", qMsg->qm_ActualLen);
  128.         if (inf) fprintf(output,"Info: alloced:%li allowed:%li avail:%li\n",inf->qr_Alloced, inf->qr_Allowed, inf->qr_Avail); 
  129.         if (qMsg->qm_Status == QERROR_NO_CONNECTION) Signal(FindTask(NULL), SIGBREAKF_CTRL_C);  /* kill myself! */
  130.         FreeQMessage(session, qMsg);
  131.         fflush(output);        
  132.       }
  133.     }
  134.     if (signals & SIGBREAKF_CTRL_F) ProcessDebugCommands(session);
  135.     if (signals & SIGBREAKF_CTRL_C) break;  /* Quit if CTRL-C pressed */
  136.   }
  137.   /* CleanExit() called here! */
  138. }
  139.